package com.canbot.u05.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.canbot.u05.R;

/**
 * 自定义开机语音界面组合控件
 *
 * @author qiandu
 */
public class ImageTextLayoutView extends LinearLayout {

        boolean LayoutViewisChecked = false;

        ImageView myimgbtn1, myimgbtn2;

        TextView myTextView;

        Context mContext;

        public ImageTextLayoutView(Context context) {
                this(context, null, 0);
        }

        public ImageTextLayoutView(Context context, AttributeSet attributeSet) {
                this(context, attributeSet, 0);
        }

        public ImageTextLayoutView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                mContext = context;
                this.setClickable(true);
                View myView = View.inflate(context, R.layout.image_text_layout_view, this);
                myimgbtn1 = (ImageView) myView.findViewById(R.id.cvd_imgview1);
                myimgbtn2 = (ImageView) myView.findViewById(R.id.cvd_imgview2);
                myTextView = (TextView) myView.findViewById(R.id.cvd_textview);

                TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ImageTextLayoutView, defStyle, 0);
                //float layout_marginTop = a.getDimension(R.styleable.ImageTextLayoutView_marginTop,40);

                float img1_width = a.getDimension(R.styleable.ImageTextLayoutView_img1_width, 42);
                float img1_height = a.getDimension(R.styleable.ImageTextLayoutView_img1_height, 42);
                float img1_marginLeft = a.getDimension(R.styleable.ImageTextLayoutView_img1_marginLeft, 20);
                float img1_marginTop = a.getDimension(R.styleable.ImageTextLayoutView_img1_marginTop, 14);

                float textsize = a.getDimension(R.styleable.ImageTextLayoutView_textsize, 28);
                String text = a.getString(R.styleable.ImageTextLayoutView_text);
                float text_width = a.getDimension(R.styleable.ImageTextLayoutView_text_width, 545);
                float text_height = a.getDimension(R.styleable.ImageTextLayoutView_text_height, 68);

                float img2_width = a.getDimension(R.styleable.ImageTextLayoutView_img2_width, 19);
                float img2_height = a.getDimension(R.styleable.ImageTextLayoutView_img2_height, 38);
                float img2_marginTop = a.getDimension(R.styleable.ImageTextLayoutView_img2_marginTop, 16);
                float img2_marginRight = a.getDimension(R.styleable.ImageTextLayoutView_img2_marginRight, 34);

	/*	LayoutParams imgparam = new LayoutParams(context,attrs);
		imgparam.setMargins(0, (int)layout_marginTop, 0, 0);
		myView.setLayoutParams(imgparam);
		*/
                LayoutParams imgparam1 = new LayoutParams((int) img1_width, (int) img1_height);
                imgparam1.setMargins((int) img1_marginLeft, (int) img1_marginTop, 0, 0);
                myimgbtn1.setLayoutParams(imgparam1);

                LayoutParams imgparamtext = new LayoutParams((int) text_width, (int) text_height);
                myTextView.setLayoutParams(imgparamtext);
                myTextView.setText(text);
                myTextView.setTextSize(textsize);

                LayoutParams imgparam2 = new LayoutParams((int) img2_width, (int) img2_height);
                imgparam2.setMargins(0, (int) img2_marginTop, (int) img2_marginRight, 0);
                myimgbtn2.setLayoutParams(imgparam2);
                a.recycle();
        }

        /**
         * 设置图片1和图片2的背景
         *
         * @param checkedRes
         */
        public void setImageBackground(int[] checkedRes) {
                myimgbtn1.setBackground(getResources().getDrawable(checkedRes[0]));
                myimgbtn2.setBackground(getResources().getDrawable(checkedRes[1]));
        }

        public void setOnClickListener(final ImgtextlayoutClickListener OnClickListener) {
                this.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                changeCheck();
                                OnClickListener.OnClick();
                        }
                });

        }

        /**
         * @param left
         * @param top
         * @param right
         * @param bottom
         */
        public void setTextMargins(int left, int top, int right, int bottom) {
                LayoutParams imgparam = new LayoutParams(mContext, null);
                imgparam.setMargins(left, top, right, bottom);
                myTextView.setLayoutParams(imgparam);
        }

        //
        public void setCheck(boolean LayoutViewisChecked, int[] checkedRes, int[] uncheckedRes) {
                this.LayoutViewisChecked = LayoutViewisChecked;
                if (LayoutViewisChecked) {
                        myimgbtn1.setBackground(getResources().getDrawable(checkedRes[0]));
                        myimgbtn2.setBackground(getResources().getDrawable(checkedRes[1]));
                }
                else {
                        myimgbtn1.setBackground(getResources().getDrawable(uncheckedRes[0]));
                        myimgbtn2.setBackground(getResources().getDrawable(uncheckedRes[1]));
                }
        }

        public void setCheck(boolean LayoutViewisChecked) {
                this.LayoutViewisChecked = LayoutViewisChecked;
                if (LayoutViewisChecked) {
                        myimgbtn1.setBackground(getResources().getDrawable(R.drawable.standby_icon_but01));
                        myimgbtn2.setBackground(getResources().getDrawable(R.drawable.standby_icon_buot01));
                }
                else {
                        myimgbtn1.setBackground(getResources().getDrawable(R.drawable.standby_icon_but));
                        myimgbtn2.setBackground(getResources().getDrawable(R.drawable.icon_buot));
                }
        }

        public void changeCheck() {
                if (LayoutViewisChecked) {
                        myimgbtn1.setBackground(getResources().getDrawable(R.drawable.standby_icon_but));
                        myimgbtn2.setBackground(getResources().getDrawable(R.drawable.icon_buot));
                }
                else {

                        myimgbtn1.setBackground(getResources().getDrawable(R.drawable.standby_icon_but01));
                        myimgbtn2.setBackground(getResources().getDrawable(R.drawable.standby_icon_buot01));
                }
                LayoutViewisChecked = !LayoutViewisChecked;
        }

        public boolean isChecked() {

                return LayoutViewisChecked;
        }

        public interface ImgtextlayoutClickListener {

                public void OnClick();

        }

}